import { deleteFromArray, shuffle } from 'common/js/utils'
// import Test from '../../../static/tsconfig.json'

const mode = parseInt(localStorage.getItem('mode') || 1)

const state = {
  showPlaylist: false, // 显示播放列表
  currentMusic: {}, //  当前播放的音乐
  currentTime: 0, //  播放器时间
  lyric: null,
  lyricCurrent: null,
  playing: false, //  播放状态
  mode, //  0 单曲循环   1 歌单循环   2随机播放
  playList: [], //  播放列表
  playList2: [] //  随机播放列表
}

const actions = {
  change_state ({ commit }, data) {
    if (data.key) {
      commit('CHANGE_STATE', data)
    }
  },
  setCurrentMusic ({ commit }, music) {
    console.log(music)
    commit('SET_CURRENT_MUSIC', music)
  },
  setCurrentTime ({ commit }, time) {
    commit('SET_CURRENT_TIME', time)
  },
  setLyric ({ commit }, lyric) {
    commit('SET_LYRIC', lyric)
  },
  setLyricCurrent ({ commit }, lyricCurrent) {
    commit('SET_LYRIC_CURRENT', lyricCurrent)
  },
  setPlaying ({ commit }, value) {
    if (typeof value === 'boolean') commit('CHANGE_STATE', { key: 'playing', value })
  },
  setPlayList ({ commit }, list) {
    if (Array.isArray(list)) {
      const playList = list.concat([])
      const playList2 = shuffle(playList)
      commit('SET_PLAY_LIST', { playList, playList2 })
    }
  },
  addOneSong ({ commit, state, dispatch }, song) {
    let {currentMusic, playList, playList2} = state
    const currentIndex = playList.findIndex(item => item.id === currentMusic.id)
    const currentIndex2 = playList2.findIndex(item => item.id === currentMusic.id)

    if (currentIndex > -1) {
      playList.splice(currentIndex, 0, song)
      playList2.splice(currentIndex2, 0, song)
    } else {
      playList = playList2 = [song]
      dispatch('setCurrentMusic', song)
    }
    commit('SET_PLAY_LIST', { playList, playList2 })
  },
  delOneSong ({ commit, state, dispatch }, song) {
    let playList = [...state.playList]
    let playList2 = [...state.playList2]

    deleteFromArray(playList, item => item.id === song.id)
    deleteFromArray(playList2, item => item.id === song.id)

    commit('SET_PLAY_LIST', { playList, playList2 })
  },
  setMode ({ commit }, type) {
    if ([0, 1, 2].includes(type)) commit('SET_MODE', type)
  },

  /* 上一首 */
  Prev ({ dispatch, state }) {
    const { currentMusic, mode, playList, playList2 } = state
    if (mode !== 2) {
      let currentIndex = playList.findIndex(item => item.id === currentMusic.id)
      if (currentIndex > 0 && currentIndex < playList.length) {
        dispatch('setCurrentMusic', playList[currentIndex - 1])
      } else {
        dispatch('setCurrentMusic', playList[playList.length - 1])
      }
    } else {
      let currentIndex = playList2.findIndex(item => item.id === currentMusic.id)
      if (currentIndex > 0 && currentIndex < playList2.length) {
        dispatch('setCurrentMusic', playList2[currentIndex - 1])
      } else {
        dispatch('setCurrentMusic', playList2[playList.length - 1])
      }
    }
  },
  /* 下一首 */
  Next ({ dispatch, state }) {
    const { currentMusic, mode, playList, playList2 } = state
    if (mode !== 2) {
      let currentIndex = playList.findIndex(item => item.id === currentMusic.id)
      if (currentIndex > -1 && currentIndex < playList.length - 1) {
        dispatch('setCurrentMusic', playList[currentIndex + 1])
      } else {
        dispatch('setCurrentMusic', playList[0])
      }
    } else {
      let currentIndex = playList2.findIndex(item => item.id === currentMusic.id)
      if (currentIndex > -1 && currentIndex < playList2.length - 1) {
        dispatch('setCurrentMusic', playList2[currentIndex + 1])
      } else {
        dispatch('setCurrentMusic', playList2[0])
      }
    }
  }
}

const mutations = {
  CHANGE_STATE: (state, { key, value }) => {
    state[key] = value
  },
  SET_CURRENT_MUSIC (state, music) {
    state.currentMusic = music
  },
  SET_CURRENT_TIME (state, time) {
    state.currentTime = time
  },
  SET_LYRIC (state, lyric) {
    state.lyric = lyric
  },
  SET_LYRIC_CURRENT (state, lyricCurrent) {
    state.lyricCurrent = lyricCurrent
  },
  SET_PLAY_LIST (state, { playList, playList2 }) {
    state.playList = playList
    state.playList2 = playList2
  },
  SET_MODE (state, type) {
    state.mode = type
    localStorage.setItem('mode', type)
  }
}

export default {
  namespaced: true,
  state,
  actions,
  mutations
}
